import {getIcons, nodeColors, fittingString} from '../../utils';
export const initData = (sourceData, cmUnid) => {
  let moduleMap = new Map();
  const nodes = [];
  const edges = [];
  sourceData?.moduleDefinitions?.map(module => {
    if(!moduleMap.has(module.cmModuleId)) {
      moduleMap.set(module.cmModuleId, module);
    }
  })
  sourceData?.resourceNodes?.map((node, index) => {
    const info = Object.assign({}, node);
    node.id = node.cm_unid;
    node.moduleDefinitions = moduleMap.get(node.cm_module_id);
    node.icon = getIcons(node.moduleDefinitions?.moduleIcon);
    node.color = nodeColors[index % nodeColors.length];
    node.info = info;
    if(cmUnid == node.cm_unid) {
      Object.assign(node, {
        label: fittingString(node.cm_name, 118),
        isRoot: true,
        collapsed: false,
        isAdd: false,
        closeActive: true,
        type: 'center-node',
        color: '#3056E3',
        size: 30, //圆的大小
        style: {
          // offset: 11,
          fill: '#4E84E9',
          cursor: 'pointer',
        },
        labelCfg: {
          position: 'bottom',
          offset: 10,
          style: {
              position: 'bottom',
              offset: 10,
              fill: '#666',
              fontSize: 12,
          },
        },
      });
    }
    nodes.push(node);
  })
  sourceData?.relationLines?.map(line => {
    line.source = line.srcId;
    line.target = line.destId;
    line.label = line.mapping;
    line.sourceAnchor = 0;
    line.targetAnchor = 0;
    if(line.srcId == cmUnid || line.destId == cmUnid ) { //中心节点
      line.type = 'circle-running';
    }
    edges.push(line);
  })
  return {nodes, edges};
}